<?php 

define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require('./classes/control/control_article.php');
require ('./configs/site_config.php');

class Article
{
	var $article_c;
	function __construct(){
	
		$this->article_c = new ArticleControl();
	}
	
	//文章管理
	function index() {
		
		
		$w_type = empty($_REQUEST['w_type']) ? 0 : (int) $_REQUEST['w_type'];//网站类型
		
		//JS处理
		if ($_REQUEST['jax_type']) {
			
			switch ($_REQUEST['jax_type']) {
				
				//修改标题
				case 'update_title':
					if (!empty($_GET['ar_id'])) {
						$affect = $this->article_c->change_info($w_type,(int)$_GET['ar_id'],trim($_GET['ar_title']),'ar_title','update_cat');
						
						if ($affect == 1) {
							//修改相关文章模板的修改时间
							 $this->article_c->updateTemplate($_GET['ar_id'], $w_type, 1);
						}
						echo $affect;
						
					}
				break;
				
				//修改点击数量
				case 'update_count':
					if(!empty($_GET['ar_id'])) {
						$this->article_c->change_info($w_type,(int)$_GET['ar_id'],$_GET['ar_count'],'click_count','update_cat');
					}
				break;
				
				//修改文章其他的
				case 'update_other':
					if(!empty($_GET['ar_id']))
					{
						switch($_GET['type']) 
						{
							case 'recommend'://修改推荐
								$value = $_GET['value'];
								$field = 'is_recommend';
								
							break;
							
							case 'hot'://修改是否热门
								$value = $_GET['value'];
								$field = 'is_hot';
							break;
						}
						$this->article_c->updateTemplate($_GET['ar_id'], $w_type, 1);         
						$this->article_c->change_info($w_type, (int) $_GET['ar_id'], $value, $field, 'update_cat');
						
						
					}
				break;
				
				//删除文章
				case 'delete_article':
					
					if(!empty($_GET['checkList']))
					{
						$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
						$this->article_c->delete_article($checkList, $_GET['type']);
					}
					
				break;
				
				//是否是重复文章
				case 'repeat_article':
					
					echo  $this->article_c->change_info($w_type, (int)$_GET['ar_id'], $_GET['ar_title'], 'ar_title', 'select_cat');
				
				break;
			}
			
			exit;
			
		} elseif ( $_REQUEST['act'] == 'add' || $_REQUEST['checkbox'] ) //新增 
		{
			//提交表单
			if (!empty($_POST['ar_title'])) {
				$post_arr = $this->filterPost($_POST, $w_type);	
					
				$insert_id = $this->article_c->operate_article($post_arr);
				
				if (!empty($insert_id) ) {
					
					//note 通过Ctrl+s保存
					if ( $_POST['save_by_ajax'] ) {
						exit( 'ok:'.$insert_id );
					}
					//修改模板修改时间
					$this->article_c->updateTemplate($insert_id, $w_type, 1);
					
					$location_herf = '/wondershare/article.php?action=index&act=edit&w_type='.$w_type.'&ar_id='.$insert_id;
					echo  '<script type="text/javascript"> 
							alert("增加成功！");
							window.location.href="'.$location_herf.'";
							</script>';
				} else {
					echo  '<script type="text/javascript"> 
							alert("增加失败！");
							</script>';
				}
				
			}
			
			if ($_REQUEST['checkbox']) {
				
				if ($_REQUEST['act'] == 'edit') {
					
					$related_arr = $this->article_c->get_related_article($w_type,(int)$_REQUEST['ar_id'],$_REQUEST['related_article'],$_REQUEST['parent_id']);
				
				} else if($_REQUEST['act'] == 'add') {
					
					
					$related_arr = $this->article_c->get_related_article($w_type,0,'',$_REQUEST['parent_id']);
				
				}
				echo  $related_arr;
			} else {
				
				$this->assign_author();
				
				$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));	
				$GLOBALS['smarty']->assign('w_type',   $w_type);
				$GLOBALS['smarty']->assign('all_tag', $this->get_checkbox(array(0=>'0'),$this->article_c->get_all_tag($w_type)));
				$GLOBALS['smarty']->assign('tile',     '增加');
				$GLOBALS['smarty']->assign('form_act',     'add');
				$GLOBALS['smarty']->assign('ar_cat_type',     cat_list(0, 0, true,0,true,' WHERE c.cat_web = '.$w_type.' AND c.cat_type = 1 AND c.cat_series = 3 ',2));
				$GLOBALS['smarty']->assign('cat_select',   cat_list(0, 0, true,0,true,' WHERE c.cat_web = '.$w_type.' AND c.cat_type = 1 ',1));
				$GLOBALS['smarty']->display('article_info.htm'); 
			}
			
		} elseif ($_REQUEST['act'] == 'edit') { //修改文章
			
			//提交表单
			if (!empty($_POST['ar_title'])) {
			
				//删除队列
				queueDelete(0, $_POST['ar_id']);
				
				$post_arr = $this->filterPost($_POST, $w_type);		
				$id = $this->article_c->operate_article($post_arr);
				if ( $id > 0) {
					
					//note 通过Ctrl+s保存
					if ( $_POST['save_by_ajax'] ) {
						exit( 'ok' );
					} else {
						//修改相关文章模板的修改时间
						$this->article_c->updateTemplate($id, $w_type, 0, 'edit');
					}
				}
				
			} else {
				//加入队列
				queueInfo($_GET['ar_id']);
				
				$this->assign_author();
						
				$article_info = $this->article_c->get_article_id_info( (int) $_GET['ar_id'] );
				$related_arr = $this->article_c->get_related_article($w_type, (int) $_GET['ar_id'], $article_info[0]['related_article'], $article_info[0]['ar_cat_id']);
				$article_tag = explode(',', $article_info[0]['ar_tag_id']);
				$all_tag = $this->article_c->get_all_tag($w_type);
				
				$cat_select = cat_list(0, $article_info[0]['ar_cat_id'], true, 0, true, ' WHERE c.cat_web = '.$w_type.' AND c.cat_type = 1 ',1);
				$GLOBALS['smarty']->assign('all_tag', $this->get_checkbox($article_tag, $all_tag));
				$GLOBALS['smarty']->assign('cat_select',  $cat_select );
				$GLOBALS['smarty']->assign('related_select',  $related_arr );
				$GLOBALS['smarty']->assign('ar_cat_type',     cat_list(0, 0, true, 0, true,' WHERE c.cat_web = '.$w_type.' AND 
																c.cat_type = 1 AND c.cat_series = 3 ',2,$article_info[0]['ar_vicecat_id']));
				$GLOBALS['smarty']->assign('article_info',   $article_info[0]);	

			}
		
			$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
			$GLOBALS['smarty']->assign('w_type',   $w_type);
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('tile',     '修改');
			$GLOBALS['smarty']->display('article_info.htm'); 
			
		} else {//列表
			
			if (!empty($w_type)) {//当网站类型为空时 不准进入文章列表
				
				$del_list = $_REQUEST['del_list'] == 1 ? 1 : 0;//是否为回收站
				//分页处理
				$page = 1;
				if (!empty($_REQUEST['page'])) {
					$page = (int)$_REQUEST['page'];
				}
		
				$limit = $_REQUEST['limit'] ? $_REQUEST['limit'] : 20;
				
				//当为搜索 ，分页，或者为点击分类进来的 
				if ($_POST['search']|| $_REQUEST['url_page'] == 1 || $_GET['count'] == 'count' || $_REQUEST['sortby']) {
					
					$ar_title = $_REQUEST['url_page'] == 1 ? urldecode($_REQUEST['ar_title']) : $_REQUEST['ar_title'];
					
					$url_info = $this->article_c->get_url_info($w_type, $page, $limit, 2, $del_list, (int)$_REQUEST['tag_id'], $_REQUEST['cat_id'], $_GET['count'],
																$_REQUEST['is_recommend'], $_REQUEST['is_hot'], $ar_title, (int)$_REQUEST['ar_id'], 
																$_REQUEST['update_user'], $_REQUEST['pub_status'], $_REQUEST['sortby'], $_REQUEST['sort']);
					
					$art_info = $this->article_c->get_article_info($w_type, 'list_s', $url_info['page_info']['start'], $limit,
																	$del_list, (int)$_REQUEST['tag_id'], $_REQUEST['cat_id'], $_GET['count'], $_REQUEST['is_recommend'],
																	$_REQUEST['is_hot'], $ar_title, (int) $_REQUEST['ar_id'], $_REQUEST['update_user'], $_REQUEST['pub_status'], 
																	$_REQUEST['sortby'], $_REQUEST['sort']);
					
					$GLOBALS['smarty']->assign('is_recommend',   (int) $_REQUEST['is_recommend']);
					$GLOBALS['smarty']->assign('pub_status',   (int) $_REQUEST['pub_status']);
					$GLOBALS['smarty']->assign('is_hot',   (int) $_REQUEST['is_hot']);
					$GLOBALS['smarty']->assign('ar_title',   stripslashes($ar_title));
					$GLOBALS['smarty']->assign('ar_id',   $_REQUEST['ar_id']);
					$GLOBALS['smarty']->assign('user',   $_REQUEST['update_user']);
					$GLOBALS['smarty']->assign('pub_status',   $_REQUEST['pub_status']);
					$GLOBALS['smarty']->assign('sortby',   $_REQUEST['sortby']);
					$GLOBALS['smarty']->assign('sort',   $_REQUEST['sort']);
				}
				//当为默认或者点击标签进来的 
				else
				{
					$url_info = $this->article_c->get_url_info($w_type, $page, $limit, 1, $del_list, (int) $_REQUEST['tag_id'], $_REQUEST['cat_id']);
					$art_info = $this->article_c->get_article_info($w_type, 'list', $url_info['page_info']['start'], $limit, $del_list, (int)$_REQUEST['tag_id'], 
																	$_REQUEST['cat_id']);
				}
				
				$GLOBALS['smarty']->assign('update_user',  $this->article_c->get_update_user($w_type));
				$GLOBALS['smarty']->assign('web_info',   get_web_info($w_type));
				$GLOBALS['smarty']->assign('w_type',   $w_type);
				$GLOBALS['smarty']->assign('tag_id',   (int) $_REQUEST['tag_id']);
				$GLOBALS['smarty']->assign('del_list',   $del_list);
				$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
				$GLOBALS['smarty']->assign('art_info', $art_info );
				$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
				$GLOBALS['smarty']->assign('cat_select',   cat_list(0, (int)$_REQUEST['cat_id'], true, 0, true, ' WHERE c.cat_web = '.$w_type.' AND c.cat_type = 1  '));
				$GLOBALS['smarty']->assign('site_config', $GLOBALS['site_config'][$w_type]['article'] );
				
				if( $_REQUEST['forselect'] ){//note 选择相关联文章页面
					
					$GLOBALS['smarty']->assign('hide_ar_id', $_GET['hide_ar_id']);//不显示本文章的信息
					$GLOBALS['smarty']->assign('selected', $_GET['selected']);
					$GLOBALS['smarty']->assign('selected_arr', explode(',', $_GET['selected']));
					$GLOBALS['smarty']->display('article_index_select.htm'); 	
				} else {
					$GLOBALS['smarty']->display('article_index.htm'); 	
				}
			}
		}
	
	}
	
	
	/**
	* 过滤post数据
	*
	* @return  string
	*/
	function filterPost($_POST, $w_type) {
		
		if ( substr($_POST['other_cat_id'], -1) == ',' ){
			$other_cat_id = substr($_POST['other_cat_id'], 0, strlen($_POST['other_cat_id']) - 1) ;
		} else {
			$other_cat_id = $_POST['other_cat_id'];
		}
	
		if ( substr($_POST['hi_related_article'], -1) == ',' ) {
			$related_article  = substr($_POST['hi_related_article'], 0, strlen($_POST['hi_related_article'])-1);
		} else {
			$related_article = $_POST['hi_related_article'];
		}
	
		if (substr($_POST['ar_tag_id'], -1) == ',' ) {
			$ar_tag_id = substr($_POST['ar_tag_id'], 0, strlen($_POST['ar_tag_id'])-1);
			
		} else {
			$ar_tag_id = $_POST['ar_tag_id'];
		}
			
		$post_arr = array(  'ar_id' => (int) $_POST['ar_id'],
							'w_type' => $w_type, 
							'act_type' => $_REQUEST['act'],
							'other_cat_id' => $other_cat_id,
							'ar_image' => trim($_POST['ar_image']),
							'ar_title' => trim($_POST['ar_title']),
							'ar_subtitle' => trim($_POST['ar_subtitle']),
							'parent_id' => trim($_POST['parent_id']),	
							'is_recommend' => $_POST['is_recommend'],
							'is_hot' => $_POST['is_hot'],
							'click_count' => (int) $_POST['click_count'],
							'author_id' => (int) $_POST['author_id'],
							'ar_introduction' => trim($_POST['ar_introduction']),
							'ar_content' => trim($_POST['ar_content']),
							'ar_tag_id' => $ar_tag_id,	
							'related_article' => $related_article,
		);
		
		return $post_arr;
	}
	
	//note 将作者信息赋值到模板页面中
	function assign_author(){		
		require('./classes/module/module_author.php');
		$author_mod = new AuthorModule();
		$authors = $author_mod->get_all_list();
		$GLOBALS['smarty']->assign('authors',  $authors );
	}
	
	
	/**
	* 得到checkbox组合
	*
	* @param   string        $article_tag       文章的标签
	* @param   string        $all_tag           所有的标签
	* @return  string
	*/
	function get_checkbox($article_tag, $all_tag) {
		
		$str = '';
		for ($i=0; $i<count($all_tag); $i++) {
			
			$fage = 1;
			foreach ($article_tag AS $key => $value) {
				
				if ( $value == $all_tag[$i]['tag_id'] ) {
					$fage = 0;
					$str.='<input type="checkbox" name="tag[]" value='.$all_tag[$i]['tag_id'].'  checked="checked"/>'.htmlspecialchars($all_tag[$i]['tag_name']).' &nbsp;&nbsp;';
				}
			}
			if ($fage == 1) {
				$str.='<input type="checkbox" name="tag[]" value='.$all_tag[$i]['tag_id'].'  />'.htmlspecialchars($all_tag[$i]['tag_name']).' &nbsp;&nbsp;';
		
			}
		}
		return $str;
	
	}

	//note 整站搜索替换
	function site_search() {
		
		$w_type = $_REQUEST['w_type'];
		$keyword = $_REQUEST['keyword'];
		$replace = $_REQUEST['replace'];
		$w = $_REQUEST['w'];
		$s = $_REQUEST['s'];
		
		if ( $_REQUEST['type'] ) {	
		
			$type = $_REQUEST['type'];
			$ar_id = $_REQUEST['ar_id'];
			
			$num = 0;
			$page = max(1, $_GET['page']);
			$limit = 20;
			$offset = ($page-1) * $limit;
			
			$ar_id_list = '';
			
			//整站查找
			if ( $_REQUEST['type'] == 'search' ) {
				
				$data = $this->article_c->get_site_search( $w_type, $keyword, $w, $s, $offset, $limit, $num, $ar_id_list );//print_r( $data );
				$pages = $this->article_c->get_site_search_pages( $page, ceil( $num / $limit ) );
				
				foreach ( $data as $k => $v ) {
					
					if( $s == 1 ){	//搜文本
						$c = strip_tags( $v['ar_content'] );
					} else {			//搜源文件
						$c = str_replace('<', '&lt;', $v['ar_content']);
						$c = str_replace('>', '&gt;', $c);
					}
					$i = $w == 1 ? '' : 'i';
					
					$keyword_2 = $this->article_c->preg_filter( $keyword );
					
					$data[$k]['ar_title'] = preg_replace( '|('.$keyword_2.')|'.$i, '<span style="color:red;font-weight:bold;">$1</span>', $v['ar_title']);
					$data[$k]['ar_content'] = preg_replace( '|('.$keyword_2.')|'.$i, '<span style="color:red;font-weight:bold;">$1</span>', $c);
					$data[$k]['times'] =  preg_match_all( '|'.$keyword_2.'|'.$i, $c, $arr);//$keyword在源文件中出现的次数
					$data[$k]['times_1'] =  preg_match_all( '|'.$keyword_2.'|'.$i, strip_tags( $v['ar_content'] ), $arr);//$keyword在文本中出现的次数
					$data[$k]['times_2'] = $data[$k]['times'] - $data[$k]['times_1'];		
				}
				
				$GLOBALS['smarty']->assign('data', $data);
				$GLOBALS['smarty']->assign('num', $num);
				$GLOBALS['smarty']->assign('pages', $pages);			
				$GLOBALS['smarty']->assign('ar_id_list', $ar_id_list);
			
			//整站替换
			} elseif ( $_POST['type'] == 'replace_all' ) {
				$this->article_c->site_replace_all( $w_type, $keyword, $replace, $w, $s, $_REQUEST['ar_id_list'] );
				echo '<script type="text/javascript">alert("替换成功！");document.location.href="article.php?action=site_search&w_type='.$w_type.'&keyword='.$keyword.'&replace='.$replace.'&w='.$w.'&s='.$s.'";</script>';
				exit;
			
			//替换部分
			} elseif ( $_POST['type'] == 'replace_some' ) {
				
				if ( empty( $_REQUEST['ID'] ) ) {
					echo '<script type="text/javascript">alert("请选择您要替换的文章！");document.location.href="article.php?action=site_search&w_type='.$w_type.'&keyword='.$keyword.'&replace='.$replace.'&w='.$w.'&s='.$s.'";</script>';
					exit;
				}
				
				foreach ( $_REQUEST['ID'] as $ar_id ) {
					$ret = $this->article_c->site_replace_one( $ar_id, $keyword, $replace, $w, $s );
				}
				echo '<script type="text/javascript">alert("替换成功！");document.location.href="article.php?action=site_search&w_type='.$w_type.'&keyword='.$keyword.'&replace='.$replace.'&w='.$w.'&s='.$s.'";</script>';
				exit;
			
			//替换单个,ajax形式调用
			} elseif ( $_POST['type'] == 'replace_one' ) {
				$ret = $this->article_c->site_replace_one( $ar_id, $keyword, $replace, $w, $s );
				echo $ret ? 1 : 0;
				exit;
			}		
		}
		
		$GLOBALS['smarty']->assign('w_type', $w_type);
		$GLOBALS['smarty']->assign('keyword', str_replace( '\'', '"', stripslashes( $keyword )) );
		$GLOBALS['smarty']->assign('replace', str_replace( '\'', '"', stripslashes( $replace )) );
		$GLOBALS['smarty']->assign('w', $w);
		$GLOBALS['smarty']->assign('s', $s);
		$GLOBALS['smarty']->display('article_site_search.htm'); 
	}

}

$action    = $_REQUEST['action']; 
$article   = new Article;
if (method_exists($article, $action))  {
	$article->$action();
} else {
	$article->index();
}

?>
